package foodmaterial.action;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.github.pagehelper.PageInfo;
import foodmaterial.action.valid.ObjectNullValid;
import foodmaterial.config.ChoiceDataSource;
import foodmaterial.dto.CustomerRecipePriceTemplateDto;
import foodmaterial.dto.RecipeSupplyDto;
import foodmaterial.dto.RecipeTemplateDto;
import foodmaterial.dto.RecipeTypeDto;
import foodmaterial.entity.*;
import foodmaterial.service.RecipeService;
import foodmaterial.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Size;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RequestMapping("/api/recipe")
@RestController
@CrossOrigin(allowCredentials = "true")
public class RecipeAction extends BaseAction{

    public static class Supply {
        @JsonFormat(pattern= Constants.DEFAULT_DATE_FORMAT,timezone="GMT+8")
        @ObjectNullValid(message = "菜谱供应开始日期不能为空")
        Date beginSupplyDate ;
        @JsonFormat(pattern=Constants.DEFAULT_DATE_FORMAT,timezone="GMT+8")
        @ObjectNullValid(message = "菜谱供应截至日期不能为空")
        Date endSupplyDate ;
        @Size(min= 1,message = "菜谱供应最少有一条明细")
        List<Long> recipeId ;

        public Date getBeginSupplyDate() {
            return beginSupplyDate;
        }

        public void setBeginSupplyDate(Date beginSupplyDate) {
            this.beginSupplyDate = beginSupplyDate;
        }

        public Date getEndSupplyDate() {
            return endSupplyDate;
        }

        public void setEndSupplyDate(Date endSupplyDate) {
            this.endSupplyDate = endSupplyDate;
        }

        public List<Long> getRecipeId() {
            return recipeId;
        }

        public void setRecipeId(List<Long> recipeId) {
            this.recipeId = recipeId;
        }
    }

    @Autowired
    private RecipeService service ;

    @PostMapping("/addRecipeType")
    @ChoiceDataSource
    public JsonResult<String> addRecipeType(@RequestBody @Valid RecipeType recipeType,
                                                   BindingResult bindingResult) {
        JsonResult jsonResult = BindingResultUtil.error(bindingResult) ;
        if (jsonResult.getSuccess() == false) return jsonResult ;
        String result = service.addRecipeType(recipeType) ;
        return StringUtils.isEmpty(result) ? JsonResultTool.get(jsonResult,true, "新增菜谱分类成功") :
                JsonResultTool.get(jsonResult,false,result) ;
    }

    @PostMapping("/modifyRecipeType")
    @ChoiceDataSource
    public JsonResult<String> modifyRecipeType(@RequestBody @Valid RecipeType recipeType,
                                          BindingResult bindingResult) {
        JsonResult jsonResult = BindingResultUtil.error(bindingResult) ;
        if (jsonResult.getSuccess() == false) return jsonResult ;
        String result = service.modifyRecipeType(recipeType) ;
        return StringUtils.isEmpty(result) ? JsonResultTool.get(jsonResult,true, "修改菜谱分类成功") :
                JsonResultTool.get(jsonResult,false,result) ;
    }

    @PostMapping("/queryRecipeType")
    @ChoiceDataSource
    public JsonResult<PageInfo<RecipeType>> queryRecipeType(@RequestBody @Valid RecipeTypeDto dto,BindingResult bindingResult) {
        JsonResult jsonResult = BindingResultUtil.error(bindingResult) ;
        if (jsonResult.getSuccess() == false) return jsonResult ;
        setPageHelper(dto.getPageNo(),dto.getPageSize()) ;
        List<RecipeType> result = service.queryRecipeType(dto) ;
        PageInfo<RecipeType> pageInfo = new PageInfo<>(result) ;
        jsonResult.setData(pageInfo);
        jsonResult.setErrorMsg(ResultCode.SUCCESS.getMessage());
        return jsonResult ;
    }

    @PostMapping("/addRecipeTemplate")
    @ChoiceDataSource
    public JsonResult<String> addRecipeTemplate(@RequestBody @Valid RecipeTemplate recipeTemplate,
                                          BindingResult bindingResult) {
        JsonResult jsonResult = BindingResultUtil.error(bindingResult) ;
        if (jsonResult.getSuccess() == false) return jsonResult ;
        int result = service.addRecipeTemplate(recipeTemplate) ;
        return result > 0 ? JsonResultTool.get(jsonResult,true, "新增菜谱成功") :
                JsonResultTool.get(jsonResult,false,"新增菜谱失败") ;
    }

    @PostMapping("/modifyRecipeTemplate")
    @ChoiceDataSource
    public JsonResult<String> modifyRecipeTemplate(@RequestBody @Valid RecipeTemplate recipeTemplate,
                                              BindingResult bindingResult) {
        JsonResult jsonResult = BindingResultUtil.error(bindingResult) ;
        if (jsonResult.getSuccess() == false) return jsonResult ;
        int result = service.modifyRecipeTemplate(recipeTemplate) ;
        return result > 0 ? JsonResultTool.get(jsonResult,true, "修改菜谱成功") :
                JsonResultTool.get(jsonResult,false,"修改菜谱失败") ;
    }

    @PostMapping("/stopRecipeTemplate")
    @ChoiceDataSource
    public JsonResult<String> stopRecipeTemplate(@RequestBody RecipeTemplate recipeTemplate) {
        int result = service.stopRecipeTemplate(recipeTemplate) ;
        return result > 0 ? new JsonResult(true, "修改菜谱成功") :
                new JsonResult(false,"修改菜谱失败") ;
    }

    @PostMapping("/queryRecipeTemplate")
    @ChoiceDataSource
    public JsonResult<PageInfo<RecipeTemplate>> queryRecipeTemplate(@RequestBody @Valid RecipeTemplateDto dto,
                                   BindingResult bindingResult) {
        JsonResult jsonResult = BindingResultUtil.error(bindingResult) ;
        if (jsonResult.getSuccess() == false) return jsonResult ;
        setPageHelper(dto.getPageNo(),dto.getPageSize()) ;
        List<RecipeTemplate> result = service.queryRecipeTemplate(dto) ;
        PageInfo<RecipeTemplate> pageInfo = new PageInfo<>(result) ;
        jsonResult.setData(pageInfo);
        jsonResult.setErrorMsg(ResultCode.SUCCESS.getMessage());
        return jsonResult ;
    }

    @GetMapping("/queryRecipeTemplateDetails")
    @ChoiceDataSource
    public JsonResult<List<RecipeTemplateDetails>> queryRecipeTemplateDetails(
            @RequestParam("templateId") Long templateId) {
        List<RecipeTemplateDetails> result = service.queryRecipeTemplateDetails(templateId) ;
        return new JsonResult(true, ResultCode.SUCCESS,result) ;
    }

    @GetMapping("/queryRecipeTemplateById")
    @ChoiceDataSource
    public JsonResult<RecipeTemplate> queryRecipeTemplateById(
            @RequestParam("templateId") Long templateId) {
        RecipeTemplate result = service.queryRecipeTemplateById(templateId) ;
        return new JsonResult(true, ResultCode.SUCCESS,result) ;
    }

    @PostMapping("/addRecipeSupply")
    @ChoiceDataSource
    public JsonResult<String> addRecipeSupply(@RequestBody @Valid Supply supply,BindingResult bindingResult) {
        JsonResult jsonResult = BindingResultUtil.error(bindingResult) ;
        if (jsonResult.getSuccess() == false) return jsonResult ;
        String result = service.addRecipeSupply(supply) ;
        return StringUtils.isEmpty(result) ? JsonResultTool.get(jsonResult,true, "新增供应信息成功") :
                JsonResultTool.get(jsonResult,false,result) ;
    }

    @PostMapping("/removeRecipeSupply")
    @ChoiceDataSource
    public JsonResult<String> removeRecipeSupply(@RequestBody RecipeSupplyDto dto) {
        String result = service.removeRecipeSupply(dto) ;
        return StringUtils.isEmpty(result) ? new JsonResult(true, "删除成功") :
                new JsonResult(false,result)  ;
    }

    @PostMapping("/queryRecipeSupply")
    @ChoiceDataSource
    public JsonResult<PageInfo<List<RecipeSupply>>> queryRecipeSupply(@RequestBody @Valid RecipeSupplyDto dto
            ,BindingResult bindingResult) {
        JsonResult jsonResult = BindingResultUtil.error(bindingResult) ;
        if (jsonResult.getSuccess() == false) return jsonResult ;
        this.setPageHelper(dto.getPageNo(),dto.getPageSize());
        List<RecipeSupply> result = service.queryRecipeSupply(dto) ;
        PageInfo<RecipeSupply> pageInfo = new PageInfo<>(result) ;
        jsonResult.setData(pageInfo);
        jsonResult.setErrorMsg(ResultCode.SUCCESS.getMessage());
        return jsonResult ;
    }

    @PostMapping("/queryRecipe")
    @ChoiceDataSource
    public JsonResult<PageInfo<List<RecipeTemplate>>> queryRecipe(@RequestBody RecipeTemplateDto dto)  {
        PageInfo<RecipeTemplate> result = service.queryRecipe(dto) ;
        return new JsonResult(true,ResultCode.SUCCESS,result) ;
    }

    @PostMapping("/batchModifyRecipePrice")
    @ChoiceDataSource
    public JsonResult<String> batchModifyRecipePrice(@RequestBody Map<String,List<RecipeTemplate>> recipes) {
        JsonResult jsonResult = new JsonResult() ;
        String result = service.batchModifyRecipePrice(recipes.get("recipes")) ;
        return result == null ? JsonResultTool.get(jsonResult,true, "批量修改成功") :
                JsonResultTool.get(jsonResult,false,result) ;
    }

    @PostMapping("/addCustomerRecipePriceTemplate")
    @ChoiceDataSource
    public JsonResult<String> addCustomerRecipePriceTemplate(
            @RequestBody @Valid CustomerRecipePriceTemplate customerPriceTemplate,BindingResult bindingResult) {
        JsonResult jsonResult = BindingResultUtil.error(bindingResult) ;
        if (jsonResult.getSuccess() == false) return jsonResult ;
        int result = service.addCustomerRecipePriceTemplate(customerPriceTemplate) ;
        return result > 0 ? JsonResultTool.get(jsonResult,true, "新增价格模板成功") :
                JsonResultTool.get(jsonResult,false,"新增价格模板失败") ;
    }

    @PostMapping("/modifyCustomerRecipePriceTemplate")
    @ChoiceDataSource
    public JsonResult<String> modifyCustomerRecipePriceTemplate(
            @RequestBody @Valid CustomerRecipePriceTemplate customerPriceTemplate,BindingResult bindingResult) {
        JsonResult jsonResult = BindingResultUtil.error(bindingResult) ;
        if (jsonResult.getSuccess() == false) return jsonResult ;
        int result = service.modifyCustomerRecipePriceTemplate(customerPriceTemplate) ;
        return result > 0 ? JsonResultTool.get(jsonResult,true, "修改价格模板成功") :
                JsonResultTool.get(jsonResult,false,"修改价格模板失败") ;
    }

    @GetMapping("/removeCustomerRecipePriceTemplate")
    @ChoiceDataSource
    public JsonResult<String> removeCustomerRecipePriceTemplate(Long templateId) {
        int result = service.removeCustomerRecipePriceTemplate(templateId) ;
        return result > 0 ? new JsonResult(true, "删除价格模板成功") :
                new JsonResult(false,"删除价格模板失败") ;
    }

    @PostMapping("/queryCustomerRecipePriceTemplate")
    @ChoiceDataSource
    public JsonResult<List<CustomerRecipePriceTemplate>> queryCustomerRecipePriceTemplate(
            @RequestBody CustomerRecipePriceTemplateDto dto) {
        List<CustomerRecipePriceTemplate> result = service.queryCustomerRecipePriceTemplate(dto) ;
        return new JsonResult(true,ResultCode.SUCCESS,result) ;
    }

    @PostMapping("/queryCustomerRecipePriceTemplateDetail")
    @ChoiceDataSource
    public JsonResult<List<CustomerRecipePriceTemplateDetail>> queryCustomerRecipePriceTemplateDetail(
            @RequestBody CustomerRecipePriceTemplateDto dto) {
        List<CustomerRecipePriceTemplateDetail> result = service.queryCustomerRecipePriceTemplateDetail(dto) ;
        return new JsonResult(true,ResultCode.SUCCESS,result) ;
    }

    public static class CustomerPriceChoice {
        private Long templateId ;
        private List<Long> customerIds ;

        public Long getTemplateId() {
            return templateId;
        }

        public void setTemplateId(Long templateId) {
            this.templateId = templateId;
        }

        public List<Long> getCustomerIds() {
            return customerIds;
        }

        public void setCustomerIds(List<Long> customerIds) {
            this.customerIds = customerIds;
        }
    }

    @PostMapping("/addCustomerRecipePrice")
    @ChoiceDataSource
    public JsonResult<String> addCustomerRecipePrice(@RequestBody RecipeAction.CustomerPriceChoice customerPriceChoice) {
        int result = service.addCustomerRecipePrice(customerPriceChoice) ;
        return result > 0 ? new JsonResult(true, "客户设置价格模板成功") :
                new JsonResult(false,"客户设置价格模板失败") ;

    }

    @PostMapping("/removeCustomerRecipePrice")
    @ChoiceDataSource
    public JsonResult<String> removeCustomerRecipePrice(@RequestBody CustomerRecipePriceTemplateDto dto) {
        int result = service.removeCustomerRecipePrice(dto) ;
        return result > 0 ? new JsonResult(true, "客户删除价格模板成功") :
                new JsonResult(false,"客户删除价格模板失败") ;
    }

    @PostMapping("/queryCustomerRecipePrice")
    @ChoiceDataSource
    public JsonResult<List<CustomerRecipePrice>> queryCustomerRecipePrice(@RequestBody CustomerRecipePriceTemplateDto dto) {
        List<CustomerRecipePrice> result = service.queryCustomerRecipePrice(dto);
        return new JsonResult(true,ResultCode.SUCCESS,result) ;
    }

}
